package cn.ingenic.glasssync.screen.live;
import java.util.Locale;
import android.util.Log;
import android.os.UserHandle;
import android.net.Uri;
import android.widget.Toast;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.BroadcastReceiver;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiInfo;

import cn.ingenic.glasssync.services.SyncData;
import cn.ingenic.glasssync.services.SyncModule;
import cn.ingenic.glasssync.services.SyncException;

public class LiveModule extends SyncModule {
    private static final String LIVE_NAME = "live_module";
    private static final String TAG = "LiveModule";
    private boolean DEBUG = true;

    // key-value pairs
    private final String LIVE_MESSAGE = "live_message";
    private final String LIVE_STATUS = "live_status";    
    private final String LIVE_RTSP_URL = "live_rtsp_url";

    // glass message receiver
    private final int LIVE_MSG_CAMERA_LIVE = 0;

    // mobile message receiver
    private final int LIVE_MSG_WIFI_CONNECTED = 1000;
    private final int LIVE_MSG_WIFI_UNCONNECTED = 1001;
    private final int LIVE_MSG_LIVE_STATUS = 1002;

    // camera message
    private final String ACTION_CAMERA_LIVE_START = "com.ingenic.glass.camera.CameraLive";
    private final String ACTION_CAMERA_ACTION_SEND_URL = "com.ingenic.glass.camera.live.SEND_URL";
    private final String ACTION_CAMERA_ACTION_STATUS = "com.ingenic.glass.camera.live.STATUS";

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
	    @Override
	    public void onReceive(Context context, Intent intent) {
		if (ACTION_CAMERA_ACTION_SEND_URL.equals(intent.getAction()))
		    sendURL();
		if (ACTION_CAMERA_ACTION_STATUS.equals(intent.getAction()))
		    sendCameraStatus(intent.getStringExtra("status"));
	    }
	};

    private Context mContext;
    private static LiveModule sInstance;

    private LiveModule(Context context){
	super(LIVE_NAME, context);
	mContext = context;

	IntentFilter mIntentFilter = new IntentFilter();
	mIntentFilter.addAction(ACTION_CAMERA_ACTION_SEND_URL);
	mIntentFilter.addAction(ACTION_CAMERA_ACTION_STATUS);
	mContext.registerReceiver(mReceiver, mIntentFilter);
    }

    public static LiveModule getInstance(Context c) {
	if (null == sInstance)
	    sInstance = new LiveModule(c);
	Log.i(TAG, "new LiveModule");
	return sInstance;
    }

    @Override
    protected void onCreate() {
    }

    @Override
    protected void onRetrive(SyncData data) {
	if (DEBUG) Log.i(TAG, "onRetrive");
	int message = data.getInt(LIVE_MESSAGE);
	switch (message) {
	case LIVE_MSG_CAMERA_LIVE :
	    Intent intent = new Intent();
	    intent.setAction(ACTION_CAMERA_LIVE_START);
	    mContext.sendBroadcast(intent);
	    break;
	}
    }

    public void sendURL() {
	if (DEBUG) Log.i(TAG, "sendURL");
	SyncData data = new SyncData();
	WifiManager wifiManager = (WifiManager)mContext.getSystemService(Context.WIFI_SERVICE);
	WifiInfo wifiInfo = wifiManager.getConnectionInfo();
	if (wifiInfo == null || (wifiInfo.getIpAddress() == 0)) {
	    Log.e(TAG, "Wifi not connected, connect first");
	    data.putInt(LIVE_MESSAGE, LIVE_MSG_WIFI_UNCONNECTED);
	} else {
	    int ipAddress = wifiInfo.getIpAddress();
	    String mIPAddress = String.format(Locale.US, "%d.%d.%d.%d", (ipAddress & 0xff),
					      (ipAddress >> 8 & 0xff),(ipAddress >> 16 & 0xff),
					      (ipAddress >> 24 & 0xff)).toString();
	    if (DEBUG) Log.i(TAG, "Glass's wifi ip = " + mIPAddress);
	    String url = "rtsp://" + mIPAddress + ":8554/recorderLive";
	    data.putInt(LIVE_MESSAGE, LIVE_MSG_WIFI_CONNECTED);
	    data.putString(LIVE_RTSP_URL, url);
	}
	try {
	    send(data);
	} catch (SyncException e) {
	    Log.e(TAG, "sendWifiConnected : " + e);
	}
    }

    public void sendCameraStatus(String status) {
	if (DEBUG) Log.i(TAG, "status = " + status);

	if (status.equals("start_live")){//this message is only for ios. if some problem, please ask lirui.
	    return;
	}
	SyncData data = new SyncData();
	data.putInt(LIVE_MESSAGE, LIVE_MSG_LIVE_STATUS);
	if (status != null)
	    data.putString(LIVE_STATUS, status);
	try {
	    send(data);
	} catch (SyncException e) {
	    Log.e(TAG, "sendCameraStatus : " + e);
	}
    }
}
